﻿Partial Public Class SetMovieStartTimeDialog

    Private m_isSliderMovedByUser As Boolean = False
    Private m_StartingAt As Long
    Private m_MoviePath As String
    Private WithEvents m_UpdatesTimer As Timers.Timer

#Region "Properties"

    Public Property StartingAt() As Long
        Get
            Return m_StartingAt
        End Get
        Set(ByVal value As Long)
            m_StartingAt = value
        End Set
    End Property

    Public Property MoviePath() As String
        Get
            Return m_MoviePath
        End Get
        Set (ByVal value As String)
            m_MoviePath = value
        End Set
    End Property

#End Region

#Region "Constructors"

    Public Sub New(ByVal p_MoviePath As String)
        InitializeComponent()
        m_MoviePath = p_MoviePath

        MediaElement1.Source = New Uri(m_MoviePath)
        MediaElement1.Play()


        m_UpdatesTimer = New Timers.Timer(100)
        m_UpdatesTimer.Enabled = True
    End Sub



#End Region

#Region "MediaElement"
    Private Sub MediaElement1_MediaEnded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        MediaElement1.Stop()
    End Sub

    Private Sub MediaElement1_MediaOpened(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Try
            timelineSlider.Maximum = MediaElement1.NaturalDuration.TimeSpan.TotalMilliseconds
        Catch ex As Exception
            timelineSlider.Maximum = New TimeSpan(0, 0, 1).TotalMilliseconds
        End Try

    End Sub


    Private Sub ButtonPlay_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        MediaElement1.Play()
    End Sub

    Private Sub ButtonPause_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        MediaElement1.Pause()
    End Sub

    Private Sub ButtonStop_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        MediaElement1.Stop()
    End Sub

    Private Sub timelineSlider_ValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.RoutedPropertyChangedEventArgs(Of System.Double))
        If m_isSliderMovedByUser Then
            Dim SliderValue As Integer = CInt(timelineSlider.Value)

            Dim ts As TimeSpan = New TimeSpan(0, 0, 0, 0, SliderValue)
            MediaElement1.Position = ts
        End If


    End Sub

    Private Sub timelineSlider_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles timelineSlider.MouseDown
        m_isSliderMovedByUser = True

    End Sub


    Private Sub timelineSlider_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles timelineSlider.MouseUp
        m_isSliderMovedByUser = False
    End Sub

    Private Sub timelineSlider_PreviewMouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles timelineSlider.PreviewMouseDown
        m_isSliderMovedByUser = True
    End Sub

    Private Sub timelineSlider_PreviewMouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles timelineSlider.PreviewMouseUp
        m_isSliderMovedByUser = False
    End Sub


    Public Delegate Sub ItsTimeDel()

    Private Sub ItsTimeSlider()
        Dim l_curTimeSpan = MediaElement1.Position
        Dim l_timeSpan As New TimeSpan(l_curTimeSpan.Days, l_curTimeSpan.Hours, l_curTimeSpan.Minutes, l_curTimeSpan.Seconds, 0)
        timelineSlider.Value = l_timeSpan.TotalMilliseconds
    End Sub

    Private Sub ItsTimeLabel()
        Dim l_curTimeSpan = MediaElement1.Position

        Dim l_timeSpan As New TimeSpan(l_curTimeSpan.Days, l_curTimeSpan.Hours, l_curTimeSpan.Minutes, l_curTimeSpan.Seconds, 0)

        TextboxTime.Text = l_timeSpan.ToString

    End Sub
#End Region

    Public Sub okButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        If StartingAt = 0 Then
            If TextBoxStartingAt.Text <> String.Empty Then
                Try
                    StartingAt = TextBoxStartingAt.Text
                Catch ex As Exception
                    MsgBox("Please enter total milliseconds properly")
                End Try
            Else
                MsgBox("No Starting point selected")
            End If
        End If
        DialogResult = True
        RaiseEvent OnOkButtonClick(StartingAt)

    End Sub

    Public Delegate Sub OnOkButtonClickDel(ByVal p_StartingAt As Integer)
    Public Event OnOkButtonClick As OnOkButtonClickDel

    Private Sub m_UpdatesTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles m_UpdatesTimer.Elapsed

        Me.TextboxTime.Dispatcher.Invoke(New ItsTimeDel(AddressOf ItsTimeLabel))
        Me.timelineSlider.Dispatcher.Invoke(New ItsTimeDel(AddressOf ItsTimeSlider))

    End Sub

    Private Sub StartingAt_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        'Dim l_Pos = MediaElement1.Position
        'Dim l_Seconds = Math.Round(l_Pos.TotalMilliseconds, 0)
        'StartingAt = CInt(l_Seconds)
        'ButtonStartingAt.Content = StartingAt

    End Sub

    Private Sub ButtonStartingAt_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles ButtonStartingAt.Click
        Dim l_Pos = MediaElement1.Position
        Dim l_Seconds = Math.Round(l_Pos.TotalMilliseconds, 0)
        StartingAt = CInt(l_Seconds)
        TextBoxStartingAt.Text = StartingAt
    End Sub
End Class
